#include <windowsx.h>

#include "dxdiag_res.h"
#include "dxdiagcom.h"
#include "wine/debug.h"

WINE_DEFAULT_DEBUG_CHANNEL(dxdiag);

LRESULT CALLBACK Music_DlgProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    HBRUSH g_hbrBackground = CreateSolidBrush(RGB(255,255,255));
    switch(uMsg) {
        case WM_INITDIALOG:
            return TRUE;
        case WM_CTLCOLORSTATIC:
            if (GetDlgCtrlID((HWND) lParam) == MUSICTAB_NOTESEDIT) {
                return (LRESULT)g_hbrBackground;
            }
        default:
            break;
    }

    return FALSE;

}

BOOL MusicInfo_GetInfo(DxDiagCOM* pcom, MusicInfo* info)
{
    IDxDiagContainer* music;
    IDxDiagContainer* dmports;
    IDxDiagContainer* port;
    WCHAR query[BUF_SIZE];
    HRESULT hr;
    BOOL b;
    int i;

    /* Get the music container */
    if(FAILED(DxDiagCOM_GetContainer(pcom->root, ("DxDiag_DirectMusic"), &music)))
        return FALSE;

    /* Get the notes */
    DxDiagCOM_GetStringProperty(music, (DxDiag_LoadString(MUSIC_SZNOTESENGLISH)), &info->notes);

    /* Get the ports container */
    if(FAILED(DxDiagCOM_GetContainer(music, ("DxDiag_DirectMusicPorts"), &dmports)))
        return FALSE;

    /* Get general properties */
    DxDiagCOM_GetStringProperty(music,(DxDiag_LoadString(MUSIC_SZGMFILEPATH)),&info->dlsFilePath);

    /* Allocate space for each port */
    hr = IDxDiagContainer_GetNumberOfChildContainers(dmports, &info->numPorts);
    if (FAILED(hr))
    {
        WINE_ERR("IDxContainer_GetNumberOfChildContainers failed\n");
        return FALSE;
    }

    info->ports = HeapAlloc(GetProcessHeap(), 0, sizeof(PortInfo)*info->numPorts);

    for (i = 0; i < info->numPorts; i++)
    {
        /* Get the name of the current port */
        hr = IDxDiagContainer_EnumChildContainerNames(dmports,i,query,BUF_SIZE);

        /* Get the port container */
        IDxDiagContainer_GetChildContainer(dmports, query, &port);

        /* Get properties */
        DxDiagCOM_GetStringProperty(port, DxDiag_LoadString(MUSIC_SZDESCRIPTION), &info->ports[i].description);
        DxDiagCOM_GetBoolProperty(port, DxDiag_LoadString(MUSIC_BSOFTWARE), &b);
        info->ports[i].isSoftware = BoolToChoice(b, _T("Software"), _T("Hardware"));
        DxDiagCOM_GetBoolProperty(port, DxDiag_LoadString(MUSIC_BKERNELMODE), &b);
        info->ports[i].kernelMode = BoolToYesNo(b);
        DxDiagCOM_GetBoolProperty(port, DxDiag_LoadString(MUSIC_BOUTPUTPORT), &b);
        info->ports[i].inout = BoolToChoice(b, _T("Output"), _T("Input"));
        DxDiagCOM_GetBoolProperty(port, DxDiag_LoadString(MUSIC_BUSESDLS), &b);
        info->ports[i].supportsDLS = BoolToYesNo(b);
        DxDiagCOM_GetBoolProperty(port, DxDiag_LoadString(MUSIC_BEXTERNAL), &b);
        info->ports[i].external = BoolToYesNo(b);
        DxDiagCOM_GetBoolProperty(port, DxDiag_LoadString(MUSIC_BDEFAULTPORT), &b);
        info->ports[i].defaultPort = BoolToYesNo(b);

    }
    return TRUE;
}

void Music_DisplayData()
{
    LVCOLUMN lvc;
    LVITEM lvi;
    UINT i;
    HWND hWnd;
    WCHAR buf[BUF_SIZE];
    HWND hList = GetDlgItem(hTabDisplay, MUSICTAB_PORTSLIST);
    HWND hBox = GetDlgItem(hTabDisplay, MUSICTAB_TESTBOX);

    /* Disable the music buttons (not implemented) */
    hWnd = GetDlgItem(hTabDisplay, MUSICTAB_TESTBTN);
    Button_Enable(hWnd, FALSE);
    hWnd = GetDlgItem(hTabDisplay, MUSICTAB_ACCELBTN);
    Button_Enable(hWnd, FALSE);

    /* Display the music info */
    hWnd = GetDlgItem(hTabDisplay, MUSICTAB_DLSDATA);
    SendMessage(hWnd, WM_SETTEXT, 0, (LPARAM)musicInfo.dlsFilePath);

    /* Get the music notes */
    hWnd = GetDlgItem(hTabDisplay, MUSICTAB_NOTESEDIT);
    SendMessage(hWnd, WM_SETTEXT, 0, (LPARAM)musicInfo.notes);

    /* Add columns */
    lvc.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
    lvc.fmt = LVCFMT_LEFT;
    lvc.cx = 100;
    lvc.iSubItem = 0;

    lstrcpy(buf, _T("Description"));
    lvc.pszText = buf;
    ListView_InsertColumn(hList,0,&lvc);

    lstrcpy(buf, _T("Type"));
    lvc.pszText = buf;
    ListView_InsertColumn(hList,1,&lvc);

    lstrcpy(buf, _T("Kernel Mode"));
    lvc.pszText = buf;
    ListView_InsertColumn(hList,2,&lvc);

    lstrcpy(buf, _T("In/Out"));
    lvc.pszText = buf;
    ListView_InsertColumn(hList,3,&lvc);

    lstrcpy(buf, _T("Supports DLS"));
    lvc.pszText = buf;
    ListView_InsertColumn(hList,4,&lvc);

    lstrcpy(buf, _T("External"));
    lvc.pszText = buf;
    ListView_InsertColumn(hList,5,&lvc);

    lstrcpy(buf, _T("Default Port"));
    lvc.pszText = buf;
    ListView_InsertColumn(hList,6,&lvc);

    /* Add items */
    lvi.mask = LVIF_TEXT;
    lvi.state = 0;
    lvi.stateMask = 0;


    for (i = 0; i < musicInfo.numPorts; i++)
    {
        lvi.iItem = i;
        lvi.iSubItem = 0;
        lvi.lParam = (LPARAM) &musicInfo.ports[i];
        lvi.pszText = LPSTR_TEXTCALLBACK;
        ListView_InsertItem(hList, &lvi);

        ListView_SetItemText(hList,i,0,musicInfo.ports[i].description);
        ListView_SetItemText(hList,i,1,musicInfo.ports[i].isSoftware);
        ListView_SetItemText(hList,i,2,musicInfo.ports[i].kernelMode);
        ListView_SetItemText(hList,i,3,musicInfo.ports[i].inout);
        ListView_SetItemText(hList,i,4,musicInfo.ports[i].supportsDLS);
        ListView_SetItemText(hList,i,5,musicInfo.ports[i].external);
        ListView_SetItemText(hList,i,6,musicInfo.ports[i].defaultPort);

        SendMessage(hBox, LB_ADDSTRING, 0, (LPARAM)musicInfo.ports[i].description);
    }
}

void Music_SaveText(FILE* pFile)
{
    /* TODO */
}
